package fhp.hlhj.giantfold.servicess

import android.annotation.SuppressLint
import android.app.Service
import android.content.Intent
import android.os.Handler
import android.os.IBinder
import android.os.Message
import fhp.hlhj.giantfold.event.NetWorkEvent
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import org.greenrobot.eventbus.EventBus

/**
 * Created by Administrator on 2018\3\1 0001.
 */
class NetWorkService:Service() {
    private val handler= @SuppressLint("HandlerLeak")
    object :Handler(){
        override fun handleMessage(msg: Message?) {
            super.handleMessage(msg)
            LogUtil.log("检查网络连接")
            if (!MyUtils.isNetworkAvailable(this@NetWorkService)){
                EventBus.getDefault().post(NetWorkEvent())
            }else{
                sendEmptyMessageDelayed(1,2000)
            }
        }
    }
    override fun onCreate() {
        super.onCreate()

    }
    override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
        handler.sendEmptyMessageDelayed(1,2000)
        return super.onStartCommand(intent, flags, startId)

    }
    override fun onBind(intent: Intent?): IBinder? {

        return null
    }

    override fun onDestroy() {
        super.onDestroy()

    }
}